//
//  DetailFooter.swift
//  shop
//
//  Created by chen on 2025/5/15.
//

import SwiftUI

struct DetailFooter: View {
    
    @ObservedObject var vm: DetailViewModel
    
    @State private var loading: Bool = false
    
    var body: some View {
        HStack(spacing: 10) {
            HStack(alignment: .lastTextBaseline, spacing: 2) {
                Text("券后: ¥")
                Text("\(vm.goodsDetail.actualPrice.formatMoney())")
                    .font(.system(size: 20))
            }
            .foregroundStyle(Color.red)
            .bold()
            .font(.system(size: 15))
            
            Spacer()
            
            Button {
                openApp()
            } label: {
                HStack(alignment: .lastTextBaseline, spacing: 5) {
                    Text("预估返 ¥")
                        .bold()
                    
                    Text("\(getBackPrice().formatMoney())")
                        .bold()
                        .font(.system(size: 20))
                    
                    if loading {
                        ProgressView()
                            .progressViewStyle(CircularProgressViewStyle(tint: .white))
                    }
                }
                .padding(.vertical, 8)
                .padding(.horizontal, 5)
                .frame(maxWidth: .infinity)
                .background(Color.red)
                .cornerRadius(20)
                .foregroundStyle(Color.white)
                .font(.system(size: 15))
            }
            .buttonStyle(PlainButtonStyle())
        }
        .padding(.horizontal, 10)
        .padding(.top, 10)
        .background(.white)
        .ignoresSafeArea(edges: .bottom)
    }
    
    private func getBackPrice() -> Double {
        return vm.goodsDetail.actualPrice * (vm.goodsDetail.commissionRate / 100)
    }
    
    private func openApp() {
        if vm.goodsDetail.itemLink.isEmpty {
            return
        }
        
        Task{
            loading = true
            let appScheme: String = vm.goodsDetail.shopType == 1 ? AppScheme.tmall.rawValue : AppScheme.taobao.rawValue
            
            await NetManager.shared.processURL(vm.goodsDetail.itemLink) { (param:[String:String]?) in
                loading = false
                
                guard let param = param else {
                    UIApplication.shared.open(URL(string: vm.goodsDetail.itemLink)!, options: [:], completionHandler: nil)
                    return
                }
                
                guard let id:String = param["id"] else {
                    UIApplication.shared.open(URL(string: vm.goodsDetail.itemLink)!, options: [:], completionHandler: nil)
                    return
                }
                
                let appUrl: String = vm.goodsDetail.shopType == 1 ? "\(appScheme)tmallclient/?action:item:id=\(id)" : "\(appScheme)item.taobao.com/item.htm?id=\(id)"
                
                UIApplication.shared.open(URL(string: appUrl)!, options: [:]) { success in
                    if !success {
                        UIApplication.shared.open(URL(string: vm.goodsDetail.itemLink)!, options: [:], completionHandler: nil)
                    }
                }
            }
        }
    }
}

#Preview {
    ZStack{
        Color.detailBg.ignoresSafeArea()
        
        VStack(spacing: 0) {
            ScrollView{
                Spacer()
                Text("sdsd")
            }
            DetailFooter(vm: DetailViewModel.shared)
        }
    }
}
